/*
 * ========================================================
 * Copyright(c) 2014 杭州龙骞科技-版权所有
 * ========================================================
 * 本软件由杭州龙骞科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * http://www.hzdracom.com/
 * ========================================================
 */
package com.shwread.android.ui.page;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.shwread.android.qysw.activity.BaseBusinessActivity;
import com.shwread.android.qysw10000136.R;
import com.shwread.android.ui.dialog.NetworkDialog;
import com.shwread.httpsdk.http.callback.ActionListener;
import com.shwread.httpsdk.http.face.FeedBackAction;
import com.tgx.tina.android.ipc.framework.IUIPage;

import base.tina.core.task.infc.ITaskResult;
import logic.util.NetworkUtil;
import logic.util.Util;


/**
 * @Project： Lqyd_Android
 * @Title：FeedBackPage
 * @Description： 意见反馈
 * @Author：wayne
 * @Date：2014年8月19日下午10:17:36
 */
public class FeedBackPage
        extends
        AbstractUIPage<BaseBusinessActivity>
        implements
        OnClickListener
{
	private TextView           tvTitle;
	private Button             btnCommit;
	private Button             btnBack;
	private EditText           etFeedBack;
	private InputMethodManager inputMethodManager;
	
	/**
	 * <p>
	 * Title:
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param t
	 */
	public FeedBackPage(BaseBusinessActivity t) {
		super(t);
	}
	
	@Override
	public <E extends Activity> View createView(E e, int initializers) {
		if (null != curMyView) return curMyView;
		curMyView = bActivity.getLayoutInflater().inflate(R.layout.feed_back, null);
		inputMethodManager = (InputMethodManager) bActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
		
		initView();
		setListener();
		
		return curMyView;
	}
	
	void initView() {
		tvTitle = (TextView) curMyView.findViewById(R.id.common_header_name);
		tvTitle.setText("意见反馈");
		btnBack = (Button) curMyView.findViewById(R.id.common_header_back);
		btnBack.setVisibility(View.VISIBLE);
		btnCommit = (Button) curMyView.findViewById(R.id.common_header_commit);
		btnCommit.setVisibility(View.VISIBLE);
		etFeedBack = (EditText) curMyView.findViewById(R.id.feed_back_et);
	}
	
	void setListener() {
		btnBack.setOnClickListener(this);
		btnCommit.setOnClickListener(this);
	}
	
	@Override
	public View updateView(int toStatus, ITaskResult data) {
		return null;
	}
	
	@Override
	public void notifyView(int cmd, Bundle bundle) {
		
	}
	
	@Override
	public int enter(IUIPage<?> prePage) {
		etFeedBack.requestFocus();
		inputMethodManager.showSoftInput(etFeedBack, InputMethodManager.RESULT_UNCHANGED_SHOWN);
		return 0;
	}
	
	@Override
	public int leave(IUIPage<?> nextPage) {
		Util.hideInputMethodManager(bActivity, etFeedBack);
		return 0;
	}
	
	@Override
	public void setStatus(int status) {
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.common_header_back:
				bActivity.onBackPressed();
				break;
			case R.id.common_header_commit:
				clickCommit();
				break;
			default:
				break;
		}
	}
	
	void clickCommit() {
		String strFeed = etFeedBack.getText().toString().trim();
		if (NetworkUtil.isNetAvailable(bActivity))
		{
			if (Util.isEmpty(strFeed))
			{
				Util.showToast(bActivity, "请输入宝贵意见");
			}
			else
			{
				if (Util.isValid(strFeed))
				{
					if (strFeed.length() > 1000)
					{
						Util.showToast(bActivity, "意见超过1000字");
					}
					else
					{
						new FeedBackAction(context, strFeed, new FeedListener()).start();
					}
				}
				else
				{
					Util.showToast(bActivity, "包含非法字符");
				}
			}
		}
		else
		{
			new NetworkDialog(bActivity).show();
		}
	}
	
	class FeedListener
	        implements
	        ActionListener
	{
		
		@Override
		public void OK(Object obj) {
			
			bActivity.getUIHandler().post(new Runnable()
			{
				
				@Override
				public void run() {
					Util.showToast(bActivity, "提交成功，感谢你的反馈");
					bActivity.onBackPressed();
				}
			});
		}
		
		@Override
		public void ERROR(int resultCode, String message) {
			showResult("提交失败");
		}
		
		@Override
		public void HTTPERROR(int statusCode) {
			
		}
		
	}
	
	void showResult(final String message) {
		bActivity.getUIHandler().post(new Runnable()
		{
			
			@Override
			public void run() {
				Util.showToast(bActivity, message);
				
			}
		});
	}
}
